package com.test.testapp.app;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;


public class RDW extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rdw);

    }
    private Dialog dialog;
    private String textOverload;

    public void runParserTest(View v)
    {
        EditText kentekenEditText = (EditText)findViewById(R.id.tbKenteken);
        String kenteken = kentekenEditText.getText().toString();
        parserTask pt = new com.test.testapp.app.parserTask(kenteken)
        {
            @Override
            public void onPostExecute(ArrayList result)
            {
                ListView tv = (ListView) findViewById(R.id.listView);
                ArrayAdapter listAdapter = new ArrayAdapter<String>(RDW.this,
                        android.R.layout.simple_list_item_1, result);
                tv.setAdapter(listAdapter);
                //ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1 , result);
                //tv.setAdapter((ListAdapter) arrayAdapter);
            }
        };
        pt.execute();

    }

    public static String getJSONfromURL(String url){

        //initialize
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        //http post
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response;
            response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        }catch(Exception e){
            //   Log.e(tag, "Error in http connection "+e.toString());
        }

        //convert response to string
        try{

            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
        }catch(Exception e){
            //  Log.e(tag, "Error converting result "+e.toString());
        }

        //try parse the string to a JSON object
        try{
            jArray = new JSONObject(result);
        }catch(JSONException e){
            //    Log.e(tag, "Error parsing data "+e.toString());
        }

        //return jArray;
        return result;
    }
}

